package com.lee.mysql.util;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

/**
 * jasypt加密解密
 */
public class JasyptEncryptUtils {

    /** 加密 */
    public static String encryptionPwd(String pwd,String salt){
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();

        config.setAlgorithm("PBEWithMD5AndDES");
        //生成秘钥的公钥
        config.setPassword(salt);
        //应用配置
        encryptor.setConfig(config);

        return encryptor.encrypt(pwd);
    }

    /** 解密 */
    public static String decryptPwd(String pwd,String salt){
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();

        config.setAlgorithm("PBEWithMD5AndDES");

        //生成秘钥的公钥
        config.setPassword(salt);
        //应用配置
        encryptor.setConfig(config);

        return encryptor.decrypt(pwd);
    }




    public static void main(String[] args) {
        String salt = "7efc2e8c-45dc-413d-9396-7aa0ab221e3f";

        System.out.println(encryptionPwd("root",salt));
        System.out.println(decryptPwd("7q0xAkTmKQn2Ne9hTaAxmw==",salt));
    }
}
